package com.compus_help.service.impl;

import com.compus_help.Mapper.adminNoticeMapper;
import com.compus_help.common.PageResult;
import com.compus_help.entity.DTO.adminNoticeDTO;
import com.compus_help.entity.DTO.adminNoticePageDTO;
import com.compus_help.entity.Pojo.Notice;
import com.compus_help.service.adminNoticeServer;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class adminNoticeServiceImpl implements adminNoticeServer {

    @Autowired
    private adminNoticeMapper adminNoticeMapper;

    @Override
    public PageResult getNoticePage(adminNoticePageDTO adminNoticePageDTO) {
        //设置分页
        PageHelper.startPage(adminNoticePageDTO.getPage(),adminNoticePageDTO.getPageSize());
        //查询
        Page<Notice> page = adminNoticeMapper.getNoticePage(adminNoticePageDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addNotice(adminNoticeDTO adminNoticeDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(adminNoticeDTO,notice);

        //补全数据
        notice.setPublishTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());

        //通过判断发布时间和生效时间去设置status
        if(notice.getEffectiveTime().isBefore(notice.getPublishTime())){
            notice.setStatus("启用");
        }else if(notice.getExpireTime().isBefore(notice.getPublishTime()))
        {
            notice.setStatus("禁用");
        }
        adminNoticeMapper.addNotice(notice);
    }

    @Override
    public void updateNotice(adminNoticeDTO adminNoticeDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(adminNoticeDTO,notice);
        notice.setUpdateTime(LocalDateTime.now());
        adminNoticeMapper.updateNotice(notice);
    }

    @Override
    public void deleteNotice(List<Integer> noticeId) {
        adminNoticeMapper.deleteNotice(noticeId);
    }

    @Override
    public void updateNoticeStatus(Notice notice) {
        adminNoticeMapper.updateNoticeStatus(notice);
    }


}
